package entity.connector;

import entity.bean.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author 52571783
 */
//0 0
//1 member
//2 officer
//3 manager


public class UserConnector {
    private static int error;

    public static User connect(String id, String password){
        try {
            Connection con = ConnectionProvider.getCon();
            PreparedStatement ps = con.prepareStatement(
                    "SELECT * FROM users WHERE user_id = ?",
                    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            // check if any record find
            if (!rs.next()){
                error = 1;
                return new User(0);
            }
            if (!rs.getString("password").equals(password)){
                error = 2;
                return new User(0);
            }
            // store data from result set to static data members
            User user;
            if (rs.getString("user_role").equals("member")) {
                user = new User(1);
            } else if (rs.getString("user_role").equals("officer")) {
                // check if the officer is logged in already
                if (rs.getBoolean("login")) {
                    error = 3;
                    return new User(0);
                } else {
                    user = new User(2);
                }
            } else {
                user = new User(3);
            }
            user.setId(rs.getString("user_id"));
            user.setName(rs.getString("name"));
            user.setGender(rs.getString("gender").charAt(0));
            user.setMobile(rs.getString("mobile"));
            user.setEmail(rs.getString("email"));
            user.setPoints(rs.getInt("points"));
            if (rs.getString("birthday")!=null){
                user.setBirthday(rs.getString("birthday").substring(0,10));
            }
            if (user.getRole() == 2) {
                changeStatus(user, true);
            }
            return user;
        } catch (Exception e) {
            return new User(0);
        }
    }
    
    public static int getError(){
        return error;
    }
    
    public static void disconnect(User user){
        if (user.getRole()==2){
            changeStatus(user, false);
        }
    }
            
    private static void changeStatus(User user, boolean loginStatus) {
        try {
            Connection con = ConnectionProvider.getCon();
            PreparedStatement ps = con.prepareStatement("UPDATE users SET login = ? WHERE user_id = ?");
            ps.setBoolean(1, loginStatus);
            ps.setString(2, user.getId());
            ps.executeUpdate();
        } catch (Exception e) {
        }
    }
   /* 
    public static User getUser(String id) {
        try {
            Connection con = ConnectionProvider.getCon();
            PreparedStatement ps = con.prepareStatement(
                    "SELECT * FROM users WHERE user_id = ?",
                    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            rs.next();
            // store data from result set to static data members
            User user;
            if (rs.getString("user_role").equals("member")) {
                user = new User(1);
            } else if (rs.getString("user_role").equals("officer")) {
                // check if the officer is logged in already
                if (rs.getBoolean("login")) {
                    return null;
                } else {
                    user = new User(2);
                }
            } else {
                user = new User(3);
            }
            user.setId(rs.getString("user_id"));
            user.setName(rs.getString("name"));
            user.setGender(rs.getString("gender").charAt(0));
            if (user.getRole() == 2) {
                changeStatus(user, true);
            }
            return user;
        } catch (Exception e) {
            return null;
        }
    }*/
}